﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 角色的权限
    /// </summary>
    public class T_RoleAuthorityDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加角色权限
        /// </summary>
        public static _ReturnValue Add_RoleAuthority(List<T_RoleAuthority> list, int roleId)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return Add_RoleAuthority(conn, list, roleId);
            }
            catch { return _ReturnValue.Err; }
        }
        /// <summary>
        /// 添加角色权限，不用初始化数据库连接，由其他方法传入
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static _ReturnValue Add_RoleAuthority(SGArchiveSystem7xEntities conn, List<T_RoleAuthority> list, int roleId)
        {
            try
            {
                var query = conn.T_RoleAuthority.Select(c => c).Where(c => c.RA_RID == roleId);
                foreach(var n in list)
                {
                    bool b = false;
                    foreach(var nn in query)
                    {
                        if(n.RA_ID == nn.RA_ID)
                        {
                            nn.RA_STATUS = n.RA_STATUS;
                            b = true;
                            break;
                        }
                    }
                    if (!b)
                        conn.T_RoleAuthority.Add(n);
                }
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }
    }
}
